package main

import (
	service "a-user-demo/user_service"
	"context"
	"fmt"
	"google.golang.org/grpc"
	"net"
)

// UserService 3.1 新建结构体
type UserService struct {
	service.UnimplementedUsersServer
}

// GetUser 3.2 实现具体的方法
func (u UserService) GetUser(ctx context.Context, req *service.UserGetRequest) (*service.UserGetReply, error) {
	fmt.Println("客户端的请求参数", req)

	kk := service.User{
		Id:        "fa",
		FirstName: "fasd",
		LastName:  "fasd",
		Age:       30,
	}

	return &service.UserGetReply{
		User: &kk,
	}, nil
}

func main() {
	// 1 端口监听
	conn, err := net.Listen("tcp", ":3005")
	if err != nil {
		fmt.Println(" 1 端口监听", err)
		return
	}
	fmt.Println(" 1 端口监听", conn)

	// 2 启用grpc
	s := grpc.NewServer()
	service.RegisterUsersServer(s, &UserService{})
	err = s.Serve(conn)
	if err != nil {
		fmt.Println(err)
		return
	}
}
